package com.song.service.impl;

import com.song.constant.MessageConstant;
import com.song.context.BaseContext;
import com.song.dto.OrdersSubmitDTO;
import com.song.entity.AddressBook;
import com.song.entity.OrderDetail;
import com.song.entity.Orders;
import com.song.entity.ShoppingCart;
import com.song.exception.AddressBookBusinessException;
import com.song.mapper.AddressBookMapper;
import com.song.mapper.CartMapper;
import com.song.mapper.OrderDetailMapper;
import com.song.mapper.OrderMapper;
import com.song.service.OrderService;

import com.song.vo.OrderSubmitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    //用户提交订单
    @Transactional
    @Override
    public OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO) {
        //业务异常统一处理
        //1.用户地址簿为空 抛出异常
        AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
        if (addressBook == null) {throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);}

        //2.用户购物车数据为空 抛出异常
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
       List<ShoppingCart>  list = cartMapper.getCartCount(shoppingCart);
       if (list.isEmpty()){ throw new AddressBookBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);}

       //3.向订单表插入数据
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersSubmitDTO,orders);
       orders.setUserId(BaseContext.getCurrentId());
       orders.setOrderTime(LocalDateTime.now());
       orders.setPayStatus(Orders.UN_PAID);
       orders.setStatus(Orders.PENDING_PAYMENT);
       orders.setNumber(String.valueOf(System.currentTimeMillis()));
       orders.setPhone(addressBook.getPhone());
       orders.setConsignee(addressBook.getConsignee());
       orderMapper.addOrder(orders);

       List<OrderDetail> orderDetails = new ArrayList<>();
       //4.向订单明细表插入数据
        for (ShoppingCart cart : list) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(cart,orderDetail);
            orderDetail.setOrderId(orders.getId());

            orderDetails.add(orderDetail);
        }
        orderDetailMapper.addOrderDetail(orderDetails);

        //5.清空用户购物车数据
        cartMapper.cleanCart(BaseContext.getCurrentId());

        return OrderSubmitVO.builder()
                .id(orders.getId())
                .orderNumber(orders.getNumber())
                .orderAmount(orders.getAmount())
                .orderTime(orders.getOrderTime())
                .build();
    }

}
